package com.yunji.news.controller.admin;

import com.yunji.news.common.Result;
import com.yunji.news.utils.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传控制器
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/admin/files")
@Slf4j
public class AdminFileController {

    /**
     * 上传图片
     */
    @PostMapping("/upload/image")
    public Result<Map<String, Object>> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = FileUploadUtils.uploadImage(file);
            
            Map<String, Object> result = new HashMap<>();
            result.put("url", filePath);
            result.put("name", file.getOriginalFilename());
            result.put("size", file.getSize());
            result.put("type", file.getContentType());
            
            return Result.success(result);
        } catch (IOException e) {
            log.error("图片上传失败", e);
            return Result.error("图片上传失败：" + e.getMessage());
        } catch (IllegalArgumentException e) {
            log.warn("图片上传参数错误", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 上传文档
     */
    @PostMapping("/upload/document")
    public Result<Map<String, Object>> uploadDocument(@RequestParam("file") MultipartFile file) {
        try {
            String filePath = FileUploadUtils.uploadDocument(file);
            
            Map<String, Object> result = new HashMap<>();
            result.put("url", filePath);
            result.put("name", file.getOriginalFilename());
            result.put("size", file.getSize());
            result.put("type", file.getContentType());
            
            return Result.success(result);
        } catch (IOException e) {
            log.error("文档上传失败", e);
            return Result.error("文档上传失败：" + e.getMessage());
        } catch (IllegalArgumentException e) {
            log.warn("文档上传参数错误", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 通用文件上传
     */
    @PostMapping("/upload")
    public Result<Map<String, Object>> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(defaultValue = "others") String subDir) {
        try {
            String filePath = FileUploadUtils.uploadFile(file, subDir);
            
            Map<String, Object> result = new HashMap<>();
            result.put("url", filePath);
            result.put("name", file.getOriginalFilename());
            result.put("size", file.getSize());
            result.put("sizeFormatted", FileUploadUtils.formatFileSize(file.getSize()));
            result.put("type", file.getContentType());
            
            return Result.success(result);
        } catch (IOException e) {
            log.error("文件上传失败", e);
            return Result.error("文件上传失败：" + e.getMessage());
        } catch (IllegalArgumentException e) {
            log.warn("文件上传参数错误", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除文件
     */
    @DeleteMapping
    public Result<String> deleteFile(@RequestParam String filePath) {
        try {
            boolean success = FileUploadUtils.deleteFile(filePath);
            if (success) {
                return Result.success("文件删除成功");
            } else {
                return Result.error("文件删除失败或文件不存在");
            }
        } catch (Exception e) {
            log.error("文件删除失败", e);
            return Result.error("文件删除失败：" + e.getMessage());
        }
    }
}